package com.sinopec.user;
import com.crystaldecisions.sdk.occa.infostore.*;
import com.crystaldecisions.sdk.framework.IEnterpriseSession;
import com.crystaldecisions.sdk.framework.CrystalEnterprise;
import com.crystaldecisions.sdk.exception.SDKException;
import com.crystaldecisions.sdk.occa.pluginmgr.IPluginInfo;
import com.crystaldecisions.sdk.occa.pluginmgr.IPluginMgr;
import com.crystaldecisions.sdk.plugin.desktop.user.IUser;
import com.crystaldecisions.sdk.plugin.desktop.user.IUserAliases;
import com.crystaldecisions.sdk.plugin.desktop.usergroup.IUserGroup;

public class User {
	private static String devPrefix = "BD4~200/";
	private static String prdPrefix = "BP4~800/";
	private static String adminUser = "administrator";
	private static String pwd = "sinopechrbo00";
	private static IInfoStore boInfoStore = null;
	
	//get user Session
	private IEnterpriseSession logon(String cms){
		IEnterpriseSession boEnterpriseSession = null;
		String boAuthType = "secEnterprise";
		try {
			boEnterpriseSession = CrystalEnterprise.getSessionMgr().
			logon(adminUser,pwd,cms,boAuthType);
		}
		catch(SDKException e){
			System.out.println(e.getMessage());
		}
		return boEnterpriseSession;
	}
	/*
	 * @param boInfoStore: service of bo enterprise
	 * @param groupPrefix: the prefix of group name,eg, 020201_操作组
	 * @return the group id of group
	 * */
	private int getReportGroupId(String groupName){
		IUserGroup boGroup = null;
		try{
			boGroup = (IUserGroup)boInfoStore.query("SELECT * FROM CI_SYSTEMOBJS " +
				"WHERE SI_PROGID='CrystalEnterprise.USERGROUP' AND SI_NAME='" + groupName 
				+ "'").get(0);
		}
		catch(Exception e){
			return 0;
		}
		return boGroup.getID();
		
	}
	public static boolean isCreated(String userName,String namePrefix,int groupId){
		String name = namePrefix + userName;
		IUser boCurUser = null;
		try{
			IInfoObjects boInfoObjects = boInfoStore.query("SELECT SI_ID From CI_SYSTEMOBJECTS Where " +
					"SI_PROGID='CrystalEnterprise.User' and SI_NAME = '" + name + "'");
			boCurUser = (IUser)boInfoObjects.get(0);
			boCurUser.getGroups().add(groupId);
			boInfoStore.commit(boInfoObjects);
		}
		catch(Exception e)
		{
		     System.out.println(e.getMessage());
		     return false;
		}
		return true;
	}
	/*
	 *@param path:the excel path which contains user name,group,authorty
	 *@param cms:boe system.eg 10.1.19.141:6400 or 10.1.19.127:6400
	 * */
	public boolean create(String path,String cms,String namePrefix){
		String userList[][] = UserList.get(path);
		//bw user prefix
		IEnterpriseSession boEnterpriseSession = null;
		IPluginMgr boPluginMgr = null; //Plugin Manager returned from InfoStore
	   	IPluginInfo boUserPlugin = null; //User PluginInfo object
	   	IInfoObjects boNewInfoObjects = null; //new collection created from InfoStore
	   	IInfoObject boNewUser = null; //New User created
	   	
	   	IUser boUserInfo = null; //IUser interface to allow us to change user specific settings
	   	int groupId;
	   	String groupName;
	   	int wrongRowNum;
	   	String userName;
		boEnterpriseSession = logon(cms);	
		if (null == boEnterpriseSession){
			System.out.println("logon failed!");
			return false;
		}
		try{
			this.boInfoStore = (IInfoStore)boEnterpriseSession.getService("","InfoStore");
			for (int i=0; i<userList.length; ++i){
				groupName = userList[i][UserList.userGroup];
			    groupId = getReportGroupId(groupName);
			    userName = userList[i][UserList.userName];
				if (isCreated(userName,namePrefix,groupId)){					
              System.out.println("add user: " + namePrefix + userName + "to group:" + groupName);
				}
				else{
					boPluginMgr = boInfoStore.getPluginMgr();
					boUserPlugin = boPluginMgr.getPluginInfo("CrystalEnterprise.User");
			        boNewInfoObjects = boInfoStore.newInfoObjectCollection();
			        
			        boNewUser = boNewInfoObjects.add(boUserPlugin);
			       //Set the name and description of the User.
			        boNewUser.setTitle(namePrefix + userName);
			        boUserInfo = (IUser)boNewUser;
			        boUserInfo.setConnection(IUser.NAMED);
			       //add user group 
			        if(0 == groupId){
			        	wrongRowNum = i + 1;
			        	System.out.println("user group name is wrong: " + groupName + " excel row num is: " + wrongRowNum);
			        }
			        else{
			        	boUserInfo.getGroups().add(groupId);
			        }
					//set user alias
					IUserAliases boAliases;
					String alias_name = namePrefix + userName;
					boAliases = ((IUser)boNewInfoObjects.get(0)).getAliases();
					boAliases.addExisting("secSAPR3:" + alias_name,
										"secSAPR3:" + alias_name, false);
			        boInfoStore.commit(boNewInfoObjects);
              System.out.println("user: " + alias_name + "is created,add to group: " + groupName);
				}
			}
			return true;
		}
		catch(SDKException e){
			System.out.println(e.getMessage());
			return false;
		}
		finally
	    {	if (null != boEnterpriseSession){
	    		boEnterpriseSession.logoff();
	    	}
	    }
	}
	public static void main(String[] args){
		User boUser = new User();
		//boUser.create("C:\\/1.xls",args[0],devPrefix);
		String cms = null;
		String namePrefix = null;
		if (args[0].equals("dev")){
			cms = "10.1.19.141:6400";
			namePrefix = devPrefix;
		}
		else if(args[0].equals("prd")){
			cms = "10.1.19.134:6400";
			namePrefix = prdPrefix;
		}
		boUser.create("C:\\/1.xls",cms,namePrefix);
	}
}
